﻿using UnityEngine;

namespace ViewController.Gameplay
{
    public class Enemy : MonoBehaviour
    {
        private Trigger2DCheck _fallCheck;
        private Trigger2DCheck _groundCheck;

        private Rigidbody2D _rigidbody2D;
        private Trigger2DCheck _wallCheck;

        private void Awake()
        {
            _wallCheck = transform.Find("WallCheck").GetComponent<Trigger2DCheck>();
            _fallCheck = transform.Find("FallCheck").GetComponent<Trigger2DCheck>();
            _groundCheck = transform.Find("GroundCheck").GetComponent<Trigger2DCheck>();

            _rigidbody2D = GetComponent<Rigidbody2D>();
        }

        private void FixedUpdate()
        {
            var localScale = transform.localScale;
            var scaleX = localScale.x;

            if (_groundCheck.Triggered && _fallCheck.Triggered && !_wallCheck.Triggered)
            {
                _rigidbody2D.velocity = new Vector2(scaleX * 10, _rigidbody2D.velocity.y);
            }
            else
            {
                // 反转
                localScale.x = -scaleX;
                transform.localScale = localScale;
            }
        }
    }
}